package com.traffic.between.adaptor;

import java.util.Date;

import org.apache.log4j.Logger;

import com.traffic.between.annotation.ChooseProvider;
import com.traffic.between.config.OrderInfoContext;
import com.traffic.between.redis.running.BsmOrderResultExecutor;
import com.traffic.between.service.YunzxServiceImpl;
import com.traffic.managent.service.order.bean.OrderBean;

import net.sf.json.JSONObject;

@ChooseProvider(channel = "yunzx")
public class YunzxService implements IAdaptor {

	private static Logger log = Logger.getLogger(YunzxService.class);

	public void order() {
		
		OrderBean orderBean = OrderInfoContext.getContext();
		orderBean.setSendTime(new Date());
		String pkgNo = orderBean.getProductCode();
		String phoneNo = orderBean.getPhoneNo();
		String tradeNo = orderBean.getIds();//订购流水号
		String region = orderBean.getRegion();// 0 - 全国     1 - 省
		
		
		String result = YunzxServiceImpl.service.order(pkgNo,phoneNo,tradeNo,region);
		log.info("between- 云之讯YunzxService.order() 订购请求param<>:" + result);
		
		JSONObject json = JSONObject.fromObject(result);

		String Code = json.getString("Code");//状态码

		if ("0".equals(Code)) {
			orderBean.setStatus("11");
			orderBean.setMessage(json.getString("Message"));
			orderBean.setErrorMsg(json.getString("Message"));
			orderBean.setSuppOrderNo(json.getString("TaskID"));
		} else {
			orderBean.setStatus("8");
			orderBean.setMessage(json.getString("Message"));
			orderBean.setErrorMsg(json.getString("Message"));
		}
		
		BsmOrderResultExecutor.service.setOrder(orderBean);
	}

	public void status() {
		OrderBean orderBean = OrderInfoContext.getContext();
		String cstmOrderNo = orderBean.getIds();

		String result = YunzxServiceImpl.service.orderStatus(cstmOrderNo);
		log.info("between- 云之讯YunzxService.orderStatus() 查询响应response<> :" + result);
	
		JSONObject json = JSONObject.fromObject(result);

		String Code = json.getString("Code");

		if ("0".equals(Code)) {
			String data = json.getString("Data");
			JSONObject datajson = JSONObject.fromObject(data);
			int Status = datajson.getInt("Status");
			if ("4".equals(String.valueOf(Status))) {
				orderBean.setStatus("7");
				orderBean.setMessage(datajson.getString("ReportCode"));
				orderBean.setErrorMsg(datajson.getString("ReportCode"));
		
			}else if ("5".equals(String.valueOf(Status))) {
				orderBean.setStatus("8");
				orderBean.setMessage(datajson.getString("ReportCode"));
				orderBean.setErrorMsg(datajson.getString("ReportCode"));
			}else {
				orderBean.setStatus("1");
				orderBean.setMessage(datajson.getString("ReportCode"));
				orderBean.setErrorMsg(datajson.getString("ReportCode"));
			}
		}else{
			orderBean.setStatus("1");
			orderBean.setMessage("查询请求失败,请稍后");
			orderBean.setErrorMsg("查询请求失败,请稍后");

		} 
		BsmOrderResultExecutor.service.setOrder(orderBean);
	}
	
	
}
